{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_4_early_stop.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T81-558: Applications of Deep Neural Networks\n",
    "**Module 3: Introduction to TensorFlow**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Module 3 Material\n",
    "\n",
    "* Part 3.1: Deep Learning and Neural Network Introduction [[Video]](https://www.youtube.com/watch?v=zYnI4iWRmpc&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_1_neural_net.ipynb)\n",
    "* Part 3.2: Introduction to Tensorflow and Keras [[Video]](https://www.youtube.com/watch?v=PsE73jk55cE&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_2_keras.ipynb)\n",
    "* Part 3.3: Saving and Loading a Keras Neural Network [[Video]](https://www.youtube.com/watch?v=-9QfbGM1qGw&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_3_save_load.ipynb)\n",
    "* **Part 3.4: Early Stopping in Keras to Prevent Overfitting** [[Video]](https://www.youtube.com/watch?v=m1LNunuI2fk&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_4_early_stop.ipynb)\n",
    "* Part 3.5: Extracting Weights and Manual Calculation [[Video]](https://www.youtube.com/watch?v=7PWgx16kH8s&list=PLjy4p-07OYzulelvJ5KVaT2pDlxivl_BN) [[Notebook]](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_03_5_weights.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Google CoLab Instructions\n",
    "\n",
    "The following code ensures that Google CoLab is running the correct version of TensorFlow."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: not using Google CoLab\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    %tensorflow_version 2.x\n",
    "    COLAB = True\n",
    "    print(\"Note: using Google CoLab\")\n",
    "except:\n",
    "    print(\"Note: not using Google CoLab\")\n",
    "    COLAB = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 3.4: Early Stopping in Keras to Prevent Overfitting\n",
    "\n",
    "It can be difficult to determine how many epochs to cycle through to train a neural network. Overfitting will occur if you train the neural network for too many epochs, and the neural network will not perform well on new data, despite attaining a good accuracy on the training set. Overfitting occurs when a neural network is trained to the point that it begins to memorize rather than generalize, as demonstrated in Figure 3.OVER. \n",
    "\n",
    "**Figure 3.OVER: Training vs. Validation Error for Overfitting**\n",
    "![Training vs. Validation Error for Overfitting](https://raw.githubusercontent.com/jeffheaton/t81_558_deep_learning/master/images/class_3_training_val.png \"Training vs. Validation Error for Overfitting\")\n",
    "\n",
    "It is important to segment the original dataset into several datasets:\n",
    "\n",
    "* **Training Set**\n",
    "* **Validation Set**\n",
    "* **Holdout Set**\n",
    "\n",
    "You can construct these sets in several different ways. The following programs demonstrate some of these.\n",
    "\n",
    "The first method is a training and validation set. We use the training data to train the neural network until the validation set no longer improves. This attempts to stop at a near-optimal training point. This method will only give accurate \"out of sample\" predictions for the validation set; this is usually 20% of the data. The predictions for the training data will be overly optimistic, as these were the data that we used to train the neural network. Figure 3.VAL demonstrates how we divide the dataset.\n",
    "\n",
    "**Figure 3.VAL: Training with a Validation Set**\n",
    "![Training with a Validation Set](https://raw.githubusercontent.com/jeffheaton/t81_558_deep_learning/master/images/class_1_train_val.png \"Training with a Validation Set\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Early Stopping with Classification\n",
    "\n",
    "We will now see an example of classification training with early stopping. We will train the neural network until the error no longer improves on the validation set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 112 samples, validate on 38 samples\n",
      "Epoch 1/1000\n",
      "112/112 - 0s - loss: 1.1940 - val_loss: 1.1126\n",
      "Epoch 2/1000\n",
      "112/112 - 0s - loss: 1.0545 - val_loss: 0.9984\n",
      "Epoch 3/1000\n",
      "112/112 - 0s - loss: 0.9533 - val_loss: 0.9130\n",
      "Epoch 4/1000\n",
      "112/112 - 0s - loss: 0.8823 - val_loss: 0.8365\n",
      "Epoch 5/1000\n",
      "112/112 - 0s - loss: 0.8243 - val_loss: 0.7619\n",
      "Epoch 6/1000\n",
      "112/112 - 0s - loss: 0.7592 - val_loss: 0.7059\n",
      "Epoch 7/1000\n",
      "112/112 - 0s - loss: 0.7142 - val_loss: 0.6644\n",
      "Epoch 8/1000\n",
      "112/112 - 0s - loss: 0.6788 - val_loss: 0.6302\n",
      "Epoch 9/1000\n",
      "112/112 - 0s - loss: 0.6481 - val_loss: 0.5979\n",
      "Epoch 10/1000\n",
      "112/112 - 0s - loss: 0.6198 - val_loss: 0.5698\n",
      "Epoch 11/1000\n",
      "112/112 - 0s - loss: 0.5957 - val_loss: 0.5434\n",
      "Epoch 12/1000\n",
      "112/112 - 0s - loss: 0.5738 - val_loss: 0.5189\n",
      "Epoch 13/1000\n",
      "112/112 - 0s - loss: 0.5539 - val_loss: 0.4964\n",
      "Epoch 14/1000\n",
      "112/112 - 0s - loss: 0.5344 - val_loss: 0.4771\n",
      "Epoch 15/1000\n",
      "112/112 - 0s - loss: 0.5177 - val_loss: 0.4601\n",
      "Epoch 16/1000\n",
      "112/112 - 0s - loss: 0.5022 - val_loss: 0.4455\n",
      "Epoch 17/1000\n",
      "112/112 - 0s - loss: 0.4869 - val_loss: 0.4334\n",
      "Epoch 18/1000\n",
      "112/112 - 0s - loss: 0.4786 - val_loss: 0.4236\n",
      "Epoch 19/1000\n",
      "112/112 - 0s - loss: 0.4634 - val_loss: 0.4096\n",
      "Epoch 20/1000\n",
      "112/112 - 0s - loss: 0.4521 - val_loss: 0.3980\n",
      "Epoch 21/1000\n",
      "112/112 - 0s - loss: 0.4409 - val_loss: 0.3872\n",
      "Epoch 22/1000\n",
      "112/112 - 0s - loss: 0.4296 - val_loss: 0.3776\n",
      "Epoch 23/1000\n",
      "112/112 - 0s - loss: 0.4204 - val_loss: 0.3688\n",
      "Epoch 24/1000\n",
      "112/112 - 0s - loss: 0.4113 - val_loss: 0.3598\n",
      "Epoch 25/1000\n",
      "112/112 - 0s - loss: 0.4025 - val_loss: 0.3519\n",
      "Epoch 26/1000\n",
      "112/112 - 0s - loss: 0.3970 - val_loss: 0.3478\n",
      "Epoch 27/1000\n",
      "112/112 - 0s - loss: 0.3860 - val_loss: 0.3382\n",
      "Epoch 28/1000\n",
      "112/112 - 0s - loss: 0.3763 - val_loss: 0.3297\n",
      "Epoch 29/1000\n",
      "112/112 - 0s - loss: 0.3678 - val_loss: 0.3213\n",
      "Epoch 30/1000\n",
      "112/112 - 0s - loss: 0.3600 - val_loss: 0.3137\n",
      "Epoch 31/1000\n",
      "112/112 - 0s - loss: 0.3535 - val_loss: 0.3062\n",
      "Epoch 32/1000\n",
      "112/112 - 0s - loss: 0.3451 - val_loss: 0.2995\n",
      "Epoch 33/1000\n",
      "112/112 - 0s - loss: 0.3380 - val_loss: 0.2940\n",
      "Epoch 34/1000\n",
      "112/112 - 0s - loss: 0.3301 - val_loss: 0.2860\n",
      "Epoch 35/1000\n",
      "112/112 - 0s - loss: 0.3228 - val_loss: 0.2791\n",
      "Epoch 36/1000\n",
      "112/112 - 0s - loss: 0.3152 - val_loss: 0.2726\n",
      "Epoch 37/1000\n",
      "112/112 - 0s - loss: 0.3084 - val_loss: 0.2668\n",
      "Epoch 38/1000\n",
      "112/112 - 0s - loss: 0.3009 - val_loss: 0.2608\n",
      "Epoch 39/1000\n",
      "112/112 - 0s - loss: 0.2945 - val_loss: 0.2558\n",
      "Epoch 40/1000\n",
      "112/112 - 0s - loss: 0.2874 - val_loss: 0.2516\n",
      "Epoch 41/1000\n",
      "112/112 - 0s - loss: 0.2818 - val_loss: 0.2437\n",
      "Epoch 42/1000\n",
      "112/112 - 0s - loss: 0.2744 - val_loss: 0.2364\n",
      "Epoch 43/1000\n",
      "112/112 - 0s - loss: 0.2689 - val_loss: 0.2313\n",
      "Epoch 44/1000\n",
      "112/112 - 0s - loss: 0.2612 - val_loss: 0.2268\n",
      "Epoch 45/1000\n",
      "112/112 - 0s - loss: 0.2556 - val_loss: 0.2219\n",
      "Epoch 46/1000\n",
      "112/112 - 0s - loss: 0.2498 - val_loss: 0.2179\n",
      "Epoch 47/1000\n",
      "112/112 - 0s - loss: 0.2443 - val_loss: 0.2111\n",
      "Epoch 48/1000\n",
      "112/112 - 0s - loss: 0.2381 - val_loss: 0.2053\n",
      "Epoch 49/1000\n",
      "112/112 - 0s - loss: 0.2331 - val_loss: 0.2008\n",
      "Epoch 50/1000\n",
      "112/112 - 0s - loss: 0.2273 - val_loss: 0.1956\n",
      "Epoch 51/1000\n",
      "112/112 - 0s - loss: 0.2249 - val_loss: 0.1906\n",
      "Epoch 52/1000\n",
      "112/112 - 0s - loss: 0.2172 - val_loss: 0.1909\n",
      "Epoch 53/1000\n",
      "112/112 - 0s - loss: 0.2170 - val_loss: 0.1943\n",
      "Epoch 54/1000\n",
      "112/112 - 0s - loss: 0.2099 - val_loss: 0.1791\n",
      "Epoch 55/1000\n",
      "112/112 - 0s - loss: 0.2073 - val_loss: 0.1758\n",
      "Epoch 56/1000\n",
      "112/112 - 0s - loss: 0.2031 - val_loss: 0.1712\n",
      "Epoch 57/1000\n",
      "112/112 - 0s - loss: 0.1970 - val_loss: 0.1717\n",
      "Epoch 58/1000\n",
      "112/112 - 0s - loss: 0.1907 - val_loss: 0.1648\n",
      "Epoch 59/1000\n",
      "112/112 - 0s - loss: 0.1862 - val_loss: 0.1606\n",
      "Epoch 60/1000\n",
      "112/112 - 0s - loss: 0.1831 - val_loss: 0.1572\n",
      "Epoch 61/1000\n",
      "112/112 - 0s - loss: 0.1840 - val_loss: 0.1590\n",
      "Epoch 62/1000\n",
      "112/112 - 0s - loss: 0.1753 - val_loss: 0.1518\n",
      "Epoch 63/1000\n",
      "112/112 - 0s - loss: 0.1721 - val_loss: 0.1470\n",
      "Epoch 64/1000\n",
      "112/112 - 0s - loss: 0.1706 - val_loss: 0.1443\n",
      "Epoch 65/1000\n",
      "112/112 - 0s - loss: 0.1660 - val_loss: 0.1488\n",
      "Epoch 66/1000\n",
      "112/112 - 0s - loss: 0.1643 - val_loss: 0.1441\n",
      "Epoch 67/1000\n",
      "112/112 - 0s - loss: 0.1598 - val_loss: 0.1390\n",
      "Epoch 68/1000\n",
      "112/112 - 0s - loss: 0.1566 - val_loss: 0.1334\n",
      "Epoch 69/1000\n",
      "112/112 - 0s - loss: 0.1554 - val_loss: 0.1316\n",
      "Epoch 70/1000\n",
      "112/112 - 0s - loss: 0.1519 - val_loss: 0.1315\n",
      "Epoch 71/1000\n",
      "112/112 - 0s - loss: 0.1483 - val_loss: 0.1396\n",
      "Epoch 72/1000\n",
      "112/112 - 0s - loss: 0.1502 - val_loss: 0.1327\n",
      "Epoch 73/1000\n",
      "112/112 - 0s - loss: 0.1441 - val_loss: 0.1229\n",
      "Epoch 74/1000\n",
      "112/112 - 0s - loss: 0.1417 - val_loss: 0.1198\n",
      "Epoch 75/1000\n",
      "112/112 - 0s - loss: 0.1411 - val_loss: 0.1189\n",
      "Epoch 76/1000\n",
      "112/112 - 0s - loss: 0.1365 - val_loss: 0.1207\n",
      "Epoch 77/1000\n",
      "112/112 - 0s - loss: 0.1350 - val_loss: 0.1229\n",
      "Epoch 78/1000\n",
      "112/112 - 0s - loss: 0.1355 - val_loss: 0.1182\n",
      "Epoch 79/1000\n",
      "112/112 - 0s - loss: 0.1320 - val_loss: 0.1152\n",
      "Epoch 80/1000\n",
      "112/112 - 0s - loss: 0.1300 - val_loss: 0.1092\n",
      "Epoch 81/1000\n",
      "112/112 - 0s - loss: 0.1285 - val_loss: 0.1091\n",
      "Epoch 82/1000\n",
      "112/112 - 0s - loss: 0.1258 - val_loss: 0.1140\n",
      "Epoch 83/1000\n",
      "112/112 - 0s - loss: 0.1308 - val_loss: 0.1144\n",
      "Epoch 84/1000\n",
      "112/112 - 0s - loss: 0.1259 - val_loss: 0.1027\n",
      "Epoch 85/1000\n",
      "112/112 - 0s - loss: 0.1237 - val_loss: 0.1022\n",
      "Epoch 86/1000\n",
      "112/112 - 0s - loss: 0.1202 - val_loss: 0.1022\n",
      "Epoch 87/1000\n",
      "112/112 - 0s - loss: 0.1180 - val_loss: 0.1049\n",
      "Epoch 88/1000\n",
      "112/112 - 0s - loss: 0.1174 - val_loss: 0.1028\n",
      "Epoch 89/1000\n",
      "112/112 - 0s - loss: 0.1153 - val_loss: 0.0974\n",
      "Epoch 90/1000\n",
      "112/112 - 0s - loss: 0.1167 - val_loss: 0.0946\n",
      "Epoch 91/1000\n",
      "112/112 - 0s - loss: 0.1149 - val_loss: 0.0966\n",
      "Epoch 92/1000\n",
      "112/112 - 0s - loss: 0.1157 - val_loss: 0.1050\n",
      "Epoch 93/1000\n",
      "112/112 - 0s - loss: 0.1122 - val_loss: 0.0930\n",
      "Epoch 94/1000\n",
      "112/112 - 0s - loss: 0.1136 - val_loss: 0.0905\n",
      "Epoch 95/1000\n",
      "112/112 - 0s - loss: 0.1086 - val_loss: 0.1000\n",
      "Epoch 96/1000\n",
      "112/112 - 0s - loss: 0.1118 - val_loss: 0.1087\n",
      "Epoch 97/1000\n",
      "112/112 - 0s - loss: 0.1095 - val_loss: 0.0923\n",
      "Epoch 98/1000\n",
      "112/112 - 0s - loss: 0.1096 - val_loss: 0.0864\n",
      "Epoch 99/1000\n",
      "112/112 - 0s - loss: 0.1138 - val_loss: 0.0856\n",
      "Epoch 100/1000\n",
      "112/112 - 0s - loss: 0.1096 - val_loss: 0.1144\n",
      "Epoch 101/1000\n",
      "112/112 - 0s - loss: 0.1197 - val_loss: 0.1026\n",
      "Epoch 102/1000\n",
      "112/112 - 0s - loss: 0.1064 - val_loss: 0.0827\n",
      "Epoch 103/1000\n",
      "112/112 - 0s - loss: 0.1069 - val_loss: 0.0823\n",
      "Epoch 104/1000\n",
      "112/112 - 0s - loss: 0.1022 - val_loss: 0.0863\n",
      "Epoch 105/1000\n",
      "112/112 - 0s - loss: 0.0992 - val_loss: 0.0933\n",
      "Epoch 106/1000\n",
      "112/112 - 0s - loss: 0.1017 - val_loss: 0.0926\n",
      "Epoch 107/1000\n",
      "Restoring model weights from the end of the best epoch.\n",
      "112/112 - 0s - loss: 0.1001 - val_loss: 0.0869\n",
      "Epoch 00107: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x22a9ad34708>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import io\n",
    "import requests\n",
    "import numpy as np\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import train_test_split\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Activation\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "\n",
    "df = pd.read_csv(\n",
    "    \"https://data.heatonresearch.com/data/t81-558/iris.csv\", \n",
    "    na_values=['NA', '?'])\n",
    "\n",
    "# Convert to numpy - Classification\n",
    "x = df[['sepal_l', 'sepal_w', 'petal_l', 'petal_w']].values\n",
    "dummies = pd.get_dummies(df['species']) # Classification\n",
    "species = dummies.columns\n",
    "y = dummies.values\n",
    "\n",
    "# Split into validation and training sets\n",
    "x_train, x_test, y_train, y_test = train_test_split(    \n",
    "    x, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Build neural network\n",
    "model = Sequential()\n",
    "model.add(Dense(50, input_dim=x.shape[1], activation='relu')) # Hidden 1\n",
    "model.add(Dense(25, activation='relu')) # Hidden 2\n",
    "model.add(Dense(y.shape[1],activation='softmax')) # Output\n",
    "model.compile(loss='categorical_crossentropy', optimizer='adam')\n",
    "\n",
    "monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, \n",
    "        verbose=1, mode='auto', restore_best_weights=True)\n",
    "model.fit(x_train,y_train,validation_data=(x_test,y_test),\n",
    "        callbacks=[monitor],verbose=2,epochs=1000)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a number of parameters that are specified to the **EarlyStopping** object. \n",
    "\n",
    "* **min_delta** This value should be kept small. It simply means the minimum change in error to be registered as an improvement.  Setting it even smaller will not likely have a great deal of impact.\n",
    "* **patience** How long should the training wait for the validation error to improve?  \n",
    "* **verbose** How much progress information do you want?\n",
    "* **mode** In general, always set this to \"auto\".  This allows you to specify if the error should be minimized or maximized.  Consider accuracy, where higher numbers are desired vs log-loss/RMSE where lower numbers are desired.\n",
    "* **restore_best_weights** This should always be set to true.  This restores the weights to the values they were at when the validation set is the highest.  Unless you are manually tracking the weights yourself (we do not use this technique in this course), you should have Keras perform this step for you.\n",
    "\n",
    "As you can see from above, the entire number of requested epochs were not used.  The neural network training stopped once the validation set no longer improved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 1.0\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "pred = model.predict(x_test)\n",
    "predict_classes = np.argmax(pred,axis=1)\n",
    "expected_classes = np.argmax(y_test,axis=1)\n",
    "correct = accuracy_score(expected_classes,predict_classes)\n",
    "print(f\"Accuracy: {correct}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Early Stopping with Regression\n",
    "\n",
    "The following code demonstrates how we can apply early stopping to a regression problem.  The technique is similar to the early stopping for classification code that we just saw."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 298 samples, validate on 100 samples\n",
      "Epoch 1/1000\n",
      "298/298 - 0s - loss: 254618.1117 - val_loss: 104859.9187\n",
      "Epoch 2/1000\n",
      "298/298 - 0s - loss: 53735.2417 - val_loss: 10033.3467\n",
      "Epoch 3/1000\n",
      "298/298 - 0s - loss: 3456.0443 - val_loss: 2832.0205\n",
      "Epoch 4/1000\n",
      "298/298 - 0s - loss: 4912.1159 - val_loss: 5504.1926\n",
      "Epoch 5/1000\n",
      "298/298 - 0s - loss: 4154.7669 - val_loss: 2042.1780\n",
      "Epoch 6/1000\n",
      "298/298 - 0s - loss: 1411.5907 - val_loss: 1259.3724\n",
      "Epoch 7/1000\n",
      "298/298 - 0s - loss: 1189.8836 - val_loss: 1435.5145\n",
      "Epoch 8/1000\n",
      "298/298 - 0s - loss: 1207.4120 - val_loss: 1259.7002\n",
      "Epoch 9/1000\n",
      "298/298 - 0s - loss: 1069.7891 - val_loss: 1189.8975\n",
      "Epoch 10/1000\n",
      "298/298 - 0s - loss: 1068.2267 - val_loss: 1188.1633\n",
      "Epoch 11/1000\n",
      "298/298 - 0s - loss: 1068.9461 - val_loss: 1175.8650\n",
      "Epoch 12/1000\n",
      "298/298 - 0s - loss: 1044.6897 - val_loss: 1185.7492\n",
      "Epoch 13/1000\n",
      "298/298 - 0s - loss: 1056.0984 - val_loss: 1178.5605\n",
      "Epoch 14/1000\n",
      "298/298 - 0s - loss: 1041.7714 - val_loss: 1157.2365\n",
      "Epoch 15/1000\n",
      "298/298 - 0s - loss: 1031.7727 - val_loss: 1146.1638\n",
      "Epoch 16/1000\n",
      "298/298 - 0s - loss: 1026.6840 - val_loss: 1140.5295\n",
      "Epoch 17/1000\n",
      "298/298 - 0s - loss: 1019.7115 - val_loss: 1131.8495\n",
      "Epoch 18/1000\n",
      "298/298 - 0s - loss: 1010.8711 - val_loss: 1122.4224\n",
      "Epoch 19/1000\n",
      "298/298 - 0s - loss: 1013.6087 - val_loss: 1111.3609\n",
      "Epoch 20/1000\n",
      "298/298 - 0s - loss: 995.9503 - val_loss: 1105.5188\n",
      "Epoch 21/1000\n",
      "298/298 - 0s - loss: 987.5903 - val_loss: 1094.2863\n",
      "Epoch 22/1000\n",
      "298/298 - 0s - loss: 990.0723 - val_loss: 1089.7853\n",
      "Epoch 23/1000\n",
      "298/298 - 0s - loss: 968.7077 - val_loss: 1074.3502\n",
      "Epoch 24/1000\n",
      "298/298 - 0s - loss: 968.9280 - val_loss: 1065.4332\n",
      "Epoch 25/1000\n",
      "298/298 - 0s - loss: 955.3398 - val_loss: 1055.7287\n",
      "Epoch 26/1000\n",
      "298/298 - 0s - loss: 955.5287 - val_loss: 1052.8219\n",
      "Epoch 27/1000\n",
      "298/298 - 0s - loss: 935.7177 - val_loss: 1035.1746\n",
      "Epoch 28/1000\n",
      "298/298 - 0s - loss: 938.9435 - val_loss: 1026.7096\n",
      "Epoch 29/1000\n",
      "298/298 - 0s - loss: 921.2798 - val_loss: 1021.8623\n",
      "Epoch 30/1000\n",
      "298/298 - 0s - loss: 918.7541 - val_loss: 1021.8645\n",
      "Epoch 31/1000\n",
      "298/298 - 0s - loss: 903.5642 - val_loss: 994.2775\n",
      "Epoch 32/1000\n",
      "298/298 - 0s - loss: 896.2183 - val_loss: 984.4263\n",
      "Epoch 33/1000\n",
      "298/298 - 0s - loss: 886.1336 - val_loss: 978.4129\n",
      "Epoch 34/1000\n",
      "298/298 - 0s - loss: 877.7422 - val_loss: 964.1715\n",
      "Epoch 35/1000\n",
      "298/298 - 0s - loss: 871.3048 - val_loss: 956.3459\n",
      "Epoch 36/1000\n",
      "298/298 - 0s - loss: 861.6707 - val_loss: 948.6097\n",
      "Epoch 37/1000\n",
      "298/298 - 0s - loss: 850.0068 - val_loss: 932.7441\n",
      "Epoch 38/1000\n",
      "298/298 - 0s - loss: 846.9615 - val_loss: 921.6213\n",
      "Epoch 39/1000\n",
      "298/298 - 0s - loss: 830.3624 - val_loss: 913.5166\n",
      "Epoch 40/1000\n",
      "298/298 - 0s - loss: 831.6781 - val_loss: 907.8736\n",
      "Epoch 41/1000\n",
      "298/298 - 0s - loss: 814.4517 - val_loss: 889.8433\n",
      "Epoch 42/1000\n",
      "298/298 - 0s - loss: 804.2001 - val_loss: 879.6267\n",
      "Epoch 43/1000\n",
      "298/298 - 0s - loss: 793.5329 - val_loss: 869.0650\n",
      "Epoch 44/1000\n",
      "298/298 - 0s - loss: 786.6698 - val_loss: 857.7609\n",
      "Epoch 45/1000\n",
      "298/298 - 0s - loss: 775.7591 - val_loss: 847.1539\n",
      "Epoch 46/1000\n",
      "298/298 - 0s - loss: 767.7103 - val_loss: 836.7088\n",
      "Epoch 47/1000\n",
      "298/298 - 0s - loss: 756.9816 - val_loss: 825.8035\n",
      "Epoch 48/1000\n",
      "298/298 - 0s - loss: 747.9103 - val_loss: 819.3103\n",
      "Epoch 49/1000\n",
      "298/298 - 0s - loss: 739.1126 - val_loss: 805.0508\n",
      "Epoch 50/1000\n",
      "298/298 - 0s - loss: 734.6592 - val_loss: 795.2228\n",
      "Epoch 51/1000\n",
      "298/298 - 0s - loss: 724.3488 - val_loss: 783.2872\n",
      "Epoch 52/1000\n",
      "298/298 - 0s - loss: 710.7389 - val_loss: 779.2385\n",
      "Epoch 53/1000\n",
      "298/298 - 0s - loss: 702.9931 - val_loss: 762.7323\n",
      "Epoch 54/1000\n",
      "298/298 - 0s - loss: 694.2653 - val_loss: 751.5614\n",
      "Epoch 55/1000\n",
      "298/298 - 0s - loss: 682.2225 - val_loss: 744.4663\n",
      "Epoch 56/1000\n",
      "298/298 - 0s - loss: 683.8359 - val_loss: 738.8125\n",
      "Epoch 57/1000\n",
      "298/298 - 0s - loss: 673.9678 - val_loss: 723.7866\n",
      "Epoch 58/1000\n",
      "298/298 - 0s - loss: 655.8523 - val_loss: 715.6897\n",
      "Epoch 59/1000\n",
      "298/298 - 0s - loss: 649.6330 - val_loss: 704.0192\n",
      "Epoch 60/1000\n",
      "298/298 - 0s - loss: 643.9476 - val_loss: 691.1572\n",
      "Epoch 61/1000\n",
      "298/298 - 0s - loss: 627.9205 - val_loss: 685.6211\n",
      "Epoch 62/1000\n",
      "298/298 - 0s - loss: 630.9766 - val_loss: 675.3809\n",
      "Epoch 63/1000\n",
      "298/298 - 0s - loss: 620.4021 - val_loss: 664.9146\n",
      "Epoch 64/1000\n",
      "298/298 - 0s - loss: 601.4826 - val_loss: 655.5067\n",
      "Epoch 65/1000\n",
      "298/298 - 0s - loss: 602.5151 - val_loss: 644.4906\n",
      "Epoch 66/1000\n",
      "298/298 - 0s - loss: 584.9831 - val_loss: 631.7765\n",
      "Epoch 67/1000\n",
      "298/298 - 0s - loss: 582.3892 - val_loss: 620.7529\n",
      "Epoch 68/1000\n",
      "298/298 - 0s - loss: 580.3517 - val_loss: 617.2255\n",
      "Epoch 69/1000\n",
      "298/298 - 0s - loss: 575.3606 - val_loss: 603.6507\n",
      "Epoch 70/1000\n",
      "298/298 - 0s - loss: 551.4546 - val_loss: 598.6873\n",
      "Epoch 71/1000\n",
      "298/298 - 0s - loss: 552.0443 - val_loss: 583.6519\n",
      "Epoch 72/1000\n",
      "298/298 - 0s - loss: 536.8391 - val_loss: 576.5555\n",
      "Epoch 73/1000\n",
      "298/298 - 0s - loss: 529.9672 - val_loss: 564.6031\n",
      "Epoch 74/1000\n",
      "298/298 - 0s - loss: 522.4439 - val_loss: 556.2015\n",
      "Epoch 75/1000\n",
      "298/298 - 0s - loss: 513.4194 - val_loss: 548.1135\n",
      "Epoch 76/1000\n",
      "298/298 - 0s - loss: 505.7009 - val_loss: 537.8890\n",
      "Epoch 77/1000\n",
      "298/298 - 0s - loss: 496.8726 - val_loss: 530.3638\n",
      "Epoch 78/1000\n",
      "298/298 - 0s - loss: 488.8692 - val_loss: 520.3936\n",
      "Epoch 79/1000\n",
      "298/298 - 0s - loss: 481.2276 - val_loss: 512.5432\n",
      "Epoch 80/1000\n",
      "298/298 - 0s - loss: 477.8306 - val_loss: 503.1329\n",
      "Epoch 81/1000\n",
      "298/298 - 0s - loss: 473.3998 - val_loss: 494.9358\n",
      "Epoch 82/1000\n",
      "298/298 - 0s - loss: 465.8867 - val_loss: 490.6273\n",
      "Epoch 83/1000\n",
      "298/298 - 0s - loss: 453.1066 - val_loss: 479.8850\n",
      "Epoch 84/1000\n",
      "298/298 - 0s - loss: 445.6094 - val_loss: 471.7849\n",
      "Epoch 85/1000\n",
      "298/298 - 0s - loss: 444.9835 - val_loss: 462.8412\n",
      "Epoch 86/1000\n",
      "298/298 - 0s - loss: 443.5763 - val_loss: 456.5965\n",
      "Epoch 87/1000\n",
      "298/298 - 0s - loss: 436.6940 - val_loss: 453.5159\n",
      "Epoch 88/1000\n",
      "298/298 - 0s - loss: 414.3947 - val_loss: 447.0089\n",
      "Epoch 89/1000\n",
      "298/298 - 0s - loss: 416.7841 - val_loss: 433.9080\n",
      "Epoch 90/1000\n",
      "298/298 - 0s - loss: 403.5432 - val_loss: 423.4334\n",
      "Epoch 91/1000\n",
      "298/298 - 0s - loss: 403.1473 - val_loss: 415.1188\n",
      "Epoch 92/1000\n",
      "298/298 - 0s - loss: 390.5989 - val_loss: 408.5711\n",
      "Epoch 93/1000\n",
      "298/298 - 0s - loss: 385.0042 - val_loss: 400.7886\n",
      "Epoch 94/1000\n",
      "298/298 - 0s - loss: 380.2837 - val_loss: 394.4561\n",
      "Epoch 95/1000\n",
      "298/298 - 0s - loss: 382.1260 - val_loss: 388.9179\n",
      "Epoch 96/1000\n",
      "298/298 - 0s - loss: 371.1698 - val_loss: 380.5425\n",
      "Epoch 97/1000\n",
      "298/298 - 0s - loss: 359.9534 - val_loss: 373.7457\n",
      "Epoch 98/1000\n",
      "298/298 - 0s - loss: 358.0036 - val_loss: 366.5114\n",
      "Epoch 99/1000\n",
      "298/298 - 0s - loss: 348.6594 - val_loss: 359.0750\n",
      "Epoch 100/1000\n",
      "298/298 - 0s - loss: 344.6860 - val_loss: 352.5845\n",
      "Epoch 101/1000\n",
      "298/298 - 0s - loss: 338.0005 - val_loss: 345.9701\n",
      "Epoch 102/1000\n",
      "298/298 - 0s - loss: 331.2779 - val_loss: 340.2206\n",
      "Epoch 103/1000\n",
      "298/298 - 0s - loss: 325.3663 - val_loss: 334.1550\n",
      "Epoch 104/1000\n",
      "298/298 - 0s - loss: 319.3072 - val_loss: 327.7170\n",
      "Epoch 105/1000\n",
      "298/298 - 0s - loss: 313.7492 - val_loss: 322.3784\n",
      "Epoch 106/1000\n",
      "298/298 - 0s - loss: 313.7471 - val_loss: 315.4883\n",
      "Epoch 107/1000\n",
      "298/298 - 0s - loss: 304.8789 - val_loss: 309.6435\n",
      "Epoch 108/1000\n",
      "298/298 - 0s - loss: 301.6150 - val_loss: 304.9265\n",
      "Epoch 109/1000\n",
      "298/298 - 0s - loss: 300.2148 - val_loss: 299.9399\n",
      "Epoch 110/1000\n",
      "298/298 - 0s - loss: 289.3050 - val_loss: 292.6603\n",
      "Epoch 111/1000\n",
      "298/298 - 0s - loss: 282.8135 - val_loss: 286.8729\n",
      "Epoch 112/1000\n",
      "298/298 - 0s - loss: 283.6183 - val_loss: 281.0534\n",
      "Epoch 113/1000\n",
      "298/298 - 0s - loss: 274.6550 - val_loss: 275.6063\n",
      "Epoch 114/1000\n",
      "298/298 - 0s - loss: 269.9542 - val_loss: 271.9059\n",
      "Epoch 115/1000\n",
      "298/298 - 0s - loss: 265.6656 - val_loss: 265.1887\n",
      "Epoch 116/1000\n",
      "298/298 - 0s - loss: 262.1005 - val_loss: 260.1739\n",
      "Epoch 117/1000\n",
      "298/298 - 0s - loss: 256.3500 - val_loss: 255.2909\n",
      "Epoch 118/1000\n",
      "298/298 - 0s - loss: 251.3900 - val_loss: 252.0265\n",
      "Epoch 119/1000\n",
      "298/298 - 0s - loss: 247.2246 - val_loss: 245.5129\n",
      "Epoch 120/1000\n",
      "298/298 - 0s - loss: 241.7555 - val_loss: 240.8349\n",
      "Epoch 121/1000\n",
      "298/298 - 0s - loss: 237.9977 - val_loss: 236.3335\n",
      "Epoch 122/1000\n",
      "298/298 - 0s - loss: 233.5239 - val_loss: 231.7200\n",
      "Epoch 123/1000\n",
      "298/298 - 0s - loss: 229.3251 - val_loss: 227.2675\n",
      "Epoch 124/1000\n",
      "298/298 - 0s - loss: 225.5864 - val_loss: 222.6441\n",
      "Epoch 125/1000\n",
      "298/298 - 0s - loss: 221.2191 - val_loss: 218.1110\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 126/1000\n",
      "298/298 - 0s - loss: 217.8098 - val_loss: 213.9518\n",
      "Epoch 127/1000\n",
      "298/298 - 0s - loss: 214.3937 - val_loss: 210.5598\n",
      "Epoch 128/1000\n",
      "298/298 - 0s - loss: 210.2760 - val_loss: 205.6227\n",
      "Epoch 129/1000\n",
      "298/298 - 0s - loss: 206.5413 - val_loss: 202.4728\n",
      "Epoch 130/1000\n",
      "298/298 - 0s - loss: 202.3109 - val_loss: 197.9401\n",
      "Epoch 131/1000\n",
      "298/298 - 0s - loss: 199.8272 - val_loss: 196.1144\n",
      "Epoch 132/1000\n",
      "298/298 - 0s - loss: 197.1229 - val_loss: 190.0905\n",
      "Epoch 133/1000\n",
      "298/298 - 0s - loss: 192.5514 - val_loss: 186.7910\n",
      "Epoch 134/1000\n",
      "298/298 - 0s - loss: 189.2665 - val_loss: 184.1961\n",
      "Epoch 135/1000\n",
      "298/298 - 0s - loss: 185.1848 - val_loss: 179.9203\n",
      "Epoch 136/1000\n",
      "298/298 - 0s - loss: 186.1516 - val_loss: 176.2954\n",
      "Epoch 137/1000\n",
      "298/298 - 0s - loss: 182.4030 - val_loss: 173.4539\n",
      "Epoch 138/1000\n",
      "298/298 - 0s - loss: 177.4716 - val_loss: 169.6453\n",
      "Epoch 139/1000\n",
      "298/298 - 0s - loss: 173.9908 - val_loss: 166.0001\n",
      "Epoch 140/1000\n",
      "298/298 - 0s - loss: 173.2805 - val_loss: 162.8689\n",
      "Epoch 141/1000\n",
      "298/298 - 0s - loss: 172.0611 - val_loss: 159.8967\n",
      "Epoch 142/1000\n",
      "298/298 - 0s - loss: 165.5859 - val_loss: 157.3186\n",
      "Epoch 143/1000\n",
      "298/298 - 0s - loss: 162.3572 - val_loss: 153.6901\n",
      "Epoch 144/1000\n",
      "298/298 - 0s - loss: 161.0297 - val_loss: 151.6905\n",
      "Epoch 145/1000\n",
      "298/298 - 0s - loss: 164.1645 - val_loss: 148.8484\n",
      "Epoch 146/1000\n",
      "298/298 - 0s - loss: 156.3238 - val_loss: 145.0771\n",
      "Epoch 147/1000\n",
      "298/298 - 0s - loss: 152.3051 - val_loss: 142.4923\n",
      "Epoch 148/1000\n",
      "298/298 - 0s - loss: 149.8716 - val_loss: 140.4517\n",
      "Epoch 149/1000\n",
      "298/298 - 0s - loss: 147.7921 - val_loss: 137.0884\n",
      "Epoch 150/1000\n",
      "298/298 - 0s - loss: 144.5433 - val_loss: 134.4882\n",
      "Epoch 151/1000\n",
      "298/298 - 0s - loss: 144.0840 - val_loss: 134.6734\n",
      "Epoch 152/1000\n",
      "298/298 - 0s - loss: 142.7512 - val_loss: 129.2658\n",
      "Epoch 153/1000\n",
      "298/298 - 0s - loss: 138.6744 - val_loss: 126.8691\n",
      "Epoch 154/1000\n",
      "298/298 - 0s - loss: 136.3120 - val_loss: 125.7347\n",
      "Epoch 155/1000\n",
      "298/298 - 0s - loss: 134.8607 - val_loss: 122.1199\n",
      "Epoch 156/1000\n",
      "298/298 - 0s - loss: 132.3261 - val_loss: 120.8815\n",
      "Epoch 157/1000\n",
      "298/298 - 0s - loss: 130.2538 - val_loss: 117.9441\n",
      "Epoch 158/1000\n",
      "298/298 - 0s - loss: 127.5774 - val_loss: 116.9117\n",
      "Epoch 159/1000\n",
      "298/298 - 0s - loss: 128.5830 - val_loss: 114.8769\n",
      "Epoch 160/1000\n",
      "298/298 - 0s - loss: 123.8368 - val_loss: 112.2658\n",
      "Epoch 161/1000\n",
      "298/298 - 0s - loss: 121.8774 - val_loss: 110.3176\n",
      "Epoch 162/1000\n",
      "298/298 - 0s - loss: 121.1990 - val_loss: 108.8108\n",
      "Epoch 163/1000\n",
      "298/298 - 0s - loss: 119.1470 - val_loss: 106.4554\n",
      "Epoch 164/1000\n",
      "298/298 - 0s - loss: 117.1019 - val_loss: 104.7673\n",
      "Epoch 165/1000\n",
      "298/298 - 0s - loss: 114.4462 - val_loss: 102.9108\n",
      "Epoch 166/1000\n",
      "298/298 - 0s - loss: 113.8899 - val_loss: 100.6241\n",
      "Epoch 167/1000\n",
      "298/298 - 0s - loss: 113.7473 - val_loss: 99.1480\n",
      "Epoch 168/1000\n",
      "298/298 - 0s - loss: 109.9129 - val_loss: 98.5171\n",
      "Epoch 169/1000\n",
      "298/298 - 0s - loss: 111.6148 - val_loss: 95.8686\n",
      "Epoch 170/1000\n",
      "298/298 - 0s - loss: 109.5533 - val_loss: 97.8955\n",
      "Epoch 171/1000\n",
      "298/298 - 0s - loss: 110.5111 - val_loss: 92.7941\n",
      "Epoch 172/1000\n",
      "298/298 - 0s - loss: 110.6292 - val_loss: 96.9406\n",
      "Epoch 173/1000\n",
      "298/298 - 0s - loss: 108.2353 - val_loss: 90.7488\n",
      "Epoch 174/1000\n",
      "298/298 - 0s - loss: 103.7881 - val_loss: 88.2208\n",
      "Epoch 175/1000\n",
      "298/298 - 0s - loss: 100.4373 - val_loss: 89.0537\n",
      "Epoch 176/1000\n",
      "298/298 - 0s - loss: 100.0941 - val_loss: 85.4782\n",
      "Epoch 177/1000\n",
      "298/298 - 0s - loss: 97.8368 - val_loss: 85.8181\n",
      "Epoch 178/1000\n",
      "298/298 - 0s - loss: 95.8849 - val_loss: 83.0792\n",
      "Epoch 179/1000\n",
      "298/298 - 0s - loss: 94.7138 - val_loss: 84.0111\n",
      "Epoch 180/1000\n",
      "298/298 - 0s - loss: 93.9980 - val_loss: 80.8398\n",
      "Epoch 181/1000\n",
      "298/298 - 0s - loss: 92.4562 - val_loss: 81.9521\n",
      "Epoch 182/1000\n",
      "298/298 - 0s - loss: 91.9720 - val_loss: 81.2425\n",
      "Epoch 183/1000\n",
      "298/298 - 0s - loss: 93.9076 - val_loss: 77.1700\n",
      "Epoch 184/1000\n",
      "298/298 - 0s - loss: 92.0447 - val_loss: 76.0691\n",
      "Epoch 185/1000\n",
      "298/298 - 0s - loss: 92.4003 - val_loss: 77.9899\n",
      "Epoch 186/1000\n",
      "298/298 - 0s - loss: 87.6844 - val_loss: 73.9357\n",
      "Epoch 187/1000\n",
      "298/298 - 0s - loss: 86.4119 - val_loss: 74.5456\n",
      "Epoch 188/1000\n",
      "298/298 - 0s - loss: 85.1260 - val_loss: 73.0177\n",
      "Epoch 189/1000\n",
      "298/298 - 0s - loss: 85.2527 - val_loss: 71.2634\n",
      "Epoch 190/1000\n",
      "298/298 - 0s - loss: 84.7504 - val_loss: 73.4859\n",
      "Epoch 191/1000\n",
      "298/298 - 0s - loss: 83.9971 - val_loss: 70.3122\n",
      "Epoch 192/1000\n",
      "298/298 - 0s - loss: 82.2615 - val_loss: 68.4355\n",
      "Epoch 193/1000\n",
      "298/298 - 0s - loss: 86.2356 - val_loss: 76.1497\n",
      "Epoch 194/1000\n",
      "298/298 - 0s - loss: 82.0077 - val_loss: 70.1432\n",
      "Epoch 195/1000\n",
      "298/298 - 0s - loss: 84.0382 - val_loss: 74.0556\n",
      "Epoch 196/1000\n",
      "298/298 - 0s - loss: 79.0808 - val_loss: 65.3704\n",
      "Epoch 197/1000\n",
      "298/298 - 0s - loss: 77.5371 - val_loss: 65.6799\n",
      "Epoch 198/1000\n",
      "298/298 - 0s - loss: 76.7543 - val_loss: 63.9797\n",
      "Epoch 199/1000\n",
      "298/298 - 0s - loss: 75.4548 - val_loss: 65.2337\n",
      "Epoch 200/1000\n",
      "298/298 - 0s - loss: 75.2814 - val_loss: 62.7816\n",
      "Epoch 201/1000\n",
      "298/298 - 0s - loss: 78.7884 - val_loss: 66.5500\n",
      "Epoch 202/1000\n",
      "298/298 - 0s - loss: 74.7617 - val_loss: 62.7047\n",
      "Epoch 203/1000\n",
      "298/298 - 0s - loss: 73.3059 - val_loss: 63.5815\n",
      "Epoch 204/1000\n",
      "298/298 - 0s - loss: 73.3379 - val_loss: 60.1637\n",
      "Epoch 205/1000\n",
      "298/298 - 0s - loss: 72.8527 - val_loss: 59.5174\n",
      "Epoch 206/1000\n",
      "298/298 - 0s - loss: 71.3816 - val_loss: 58.9280\n",
      "Epoch 207/1000\n",
      "298/298 - 0s - loss: 71.0684 - val_loss: 58.5003\n",
      "Epoch 208/1000\n",
      "298/298 - 0s - loss: 69.5999 - val_loss: 58.6842\n",
      "Epoch 209/1000\n",
      "298/298 - 0s - loss: 69.6249 - val_loss: 63.3405\n",
      "Epoch 210/1000\n",
      "298/298 - 0s - loss: 70.2080 - val_loss: 56.3041\n",
      "Epoch 211/1000\n",
      "298/298 - 0s - loss: 68.7671 - val_loss: 56.1137\n",
      "Epoch 212/1000\n",
      "298/298 - 0s - loss: 67.4164 - val_loss: 56.0807\n",
      "Epoch 213/1000\n",
      "298/298 - 0s - loss: 67.4641 - val_loss: 60.8322\n",
      "Epoch 214/1000\n",
      "298/298 - 0s - loss: 70.2280 - val_loss: 55.4504\n",
      "Epoch 215/1000\n",
      "298/298 - 0s - loss: 70.7004 - val_loss: 56.4594\n",
      "Epoch 216/1000\n",
      "298/298 - 0s - loss: 69.3142 - val_loss: 66.7034\n",
      "Epoch 217/1000\n",
      "298/298 - 0s - loss: 70.9057 - val_loss: 52.7473\n",
      "Epoch 218/1000\n",
      "298/298 - 0s - loss: 63.8462 - val_loss: 53.7675\n",
      "Epoch 219/1000\n",
      "298/298 - 0s - loss: 65.2959 - val_loss: 56.9050\n",
      "Epoch 220/1000\n",
      "298/298 - 0s - loss: 63.8828 - val_loss: 52.9221\n",
      "Epoch 221/1000\n",
      "298/298 - 0s - loss: 66.2621 - val_loss: 61.4800\n",
      "Epoch 222/1000\n",
      "298/298 - 0s - loss: 66.0702 - val_loss: 51.9835\n",
      "Epoch 223/1000\n",
      "298/298 - 0s - loss: 62.1414 - val_loss: 50.4767\n",
      "Epoch 224/1000\n",
      "298/298 - 0s - loss: 60.9776 - val_loss: 51.0747\n",
      "Epoch 225/1000\n",
      "298/298 - 0s - loss: 61.1262 - val_loss: 49.3356\n",
      "Epoch 226/1000\n",
      "298/298 - 0s - loss: 59.9358 - val_loss: 56.2200\n",
      "Epoch 227/1000\n",
      "298/298 - 0s - loss: 61.8749 - val_loss: 48.5184\n",
      "Epoch 228/1000\n",
      "298/298 - 0s - loss: 59.3500 - val_loss: 49.2315\n",
      "Epoch 229/1000\n",
      "298/298 - 0s - loss: 58.7732 - val_loss: 49.6212\n",
      "Epoch 230/1000\n",
      "298/298 - 0s - loss: 59.0191 - val_loss: 47.4893\n",
      "Epoch 231/1000\n",
      "298/298 - 0s - loss: 58.5962 - val_loss: 52.0647\n",
      "Epoch 232/1000\n",
      "298/298 - 0s - loss: 57.5451 - val_loss: 47.0744\n",
      "Epoch 233/1000\n",
      "298/298 - 0s - loss: 57.4292 - val_loss: 48.5805\n",
      "Epoch 234/1000\n",
      "298/298 - 0s - loss: 57.2974 - val_loss: 46.4830\n",
      "Epoch 235/1000\n",
      "298/298 - 0s - loss: 59.5053 - val_loss: 48.0127\n",
      "Epoch 236/1000\n",
      "298/298 - 0s - loss: 57.6045 - val_loss: 48.8987\n",
      "Epoch 237/1000\n",
      "298/298 - 0s - loss: 55.6797 - val_loss: 45.2071\n",
      "Epoch 238/1000\n",
      "298/298 - 0s - loss: 54.9872 - val_loss: 46.9131\n",
      "Epoch 239/1000\n",
      "298/298 - 0s - loss: 55.2195 - val_loss: 44.9971\n",
      "Epoch 240/1000\n",
      "298/298 - 0s - loss: 54.4574 - val_loss: 47.3636\n",
      "Epoch 241/1000\n",
      "298/298 - 0s - loss: 55.7777 - val_loss: 43.9272\n",
      "Epoch 242/1000\n",
      "298/298 - 0s - loss: 56.6080 - val_loss: 43.6550\n",
      "Epoch 243/1000\n",
      "298/298 - 0s - loss: 53.3914 - val_loss: 44.0960\n",
      "Epoch 244/1000\n",
      "298/298 - 0s - loss: 53.3937 - val_loss: 46.4250\n",
      "Epoch 245/1000\n",
      "298/298 - 0s - loss: 52.5582 - val_loss: 43.4441\n",
      "Epoch 246/1000\n",
      "298/298 - 0s - loss: 52.2242 - val_loss: 42.5886\n",
      "Epoch 247/1000\n",
      "298/298 - 0s - loss: 53.1087 - val_loss: 45.4969\n",
      "Epoch 248/1000\n",
      "298/298 - 0s - loss: 51.2835 - val_loss: 42.2982\n",
      "Epoch 249/1000\n",
      "298/298 - 0s - loss: 51.9679 - val_loss: 42.0797\n",
      "Epoch 250/1000\n",
      "298/298 - 0s - loss: 50.6096 - val_loss: 41.9481\n",
      "Epoch 251/1000\n",
      "298/298 - 0s - loss: 52.3675 - val_loss: 42.1443\n",
      "Epoch 252/1000\n",
      "298/298 - 0s - loss: 52.0081 - val_loss: 41.5254\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 253/1000\n",
      "298/298 - 0s - loss: 52.4647 - val_loss: 46.1836\n",
      "Epoch 254/1000\n",
      "298/298 - 0s - loss: 49.0224 - val_loss: 40.2575\n",
      "Epoch 255/1000\n",
      "298/298 - 0s - loss: 50.8724 - val_loss: 40.5554\n",
      "Epoch 256/1000\n",
      "298/298 - 0s - loss: 48.6178 - val_loss: 40.2881\n",
      "Epoch 257/1000\n",
      "298/298 - 0s - loss: 48.1621 - val_loss: 40.1415\n",
      "Epoch 258/1000\n",
      "298/298 - 0s - loss: 47.9184 - val_loss: 39.6353\n",
      "Epoch 259/1000\n",
      "298/298 - 0s - loss: 47.7817 - val_loss: 44.1131\n",
      "Epoch 260/1000\n",
      "298/298 - 0s - loss: 48.0547 - val_loss: 38.6934\n",
      "Epoch 261/1000\n",
      "298/298 - 0s - loss: 49.1476 - val_loss: 38.5595\n",
      "Epoch 262/1000\n",
      "298/298 - 0s - loss: 48.3410 - val_loss: 38.4703\n",
      "Epoch 263/1000\n",
      "298/298 - 0s - loss: 47.1575 - val_loss: 43.8495\n",
      "Epoch 264/1000\n",
      "298/298 - 0s - loss: 47.5766 - val_loss: 37.7489\n",
      "Epoch 265/1000\n",
      "298/298 - 0s - loss: 45.9611 - val_loss: 37.8400\n",
      "Epoch 266/1000\n",
      "298/298 - 0s - loss: 45.3411 - val_loss: 37.4187\n",
      "Epoch 267/1000\n",
      "298/298 - 0s - loss: 44.8844 - val_loss: 40.0926\n",
      "Epoch 268/1000\n",
      "298/298 - 0s - loss: 45.0760 - val_loss: 36.9468\n",
      "Epoch 269/1000\n",
      "298/298 - 0s - loss: 45.1810 - val_loss: 40.3046\n",
      "Epoch 270/1000\n",
      "298/298 - 0s - loss: 44.8097 - val_loss: 37.5340\n",
      "Epoch 271/1000\n",
      "298/298 - 0s - loss: 44.2911 - val_loss: 38.6985\n",
      "Epoch 272/1000\n",
      "298/298 - 0s - loss: 43.8413 - val_loss: 37.3905\n",
      "Epoch 273/1000\n",
      "298/298 - 0s - loss: 43.3722 - val_loss: 36.7338\n",
      "Epoch 274/1000\n",
      "298/298 - 0s - loss: 43.0023 - val_loss: 35.9522\n",
      "Epoch 275/1000\n",
      "298/298 - 0s - loss: 43.3070 - val_loss: 42.2387\n",
      "Epoch 276/1000\n",
      "298/298 - 0s - loss: 43.3620 - val_loss: 35.6415\n",
      "Epoch 277/1000\n",
      "298/298 - 0s - loss: 44.2254 - val_loss: 35.0081\n",
      "Epoch 278/1000\n",
      "298/298 - 0s - loss: 43.6141 - val_loss: 35.4647\n",
      "Epoch 279/1000\n",
      "298/298 - 0s - loss: 42.5499 - val_loss: 37.3217\n",
      "Epoch 280/1000\n",
      "298/298 - 0s - loss: 42.4206 - val_loss: 36.6365\n",
      "Epoch 281/1000\n",
      "298/298 - 0s - loss: 41.8326 - val_loss: 33.9366\n",
      "Epoch 282/1000\n",
      "298/298 - 0s - loss: 40.7090 - val_loss: 35.2874\n",
      "Epoch 283/1000\n",
      "298/298 - 0s - loss: 41.1847 - val_loss: 35.7322\n",
      "Epoch 284/1000\n",
      "298/298 - 0s - loss: 40.2632 - val_loss: 33.2830\n",
      "Epoch 285/1000\n",
      "298/298 - 0s - loss: 40.4647 - val_loss: 33.4544\n",
      "Epoch 286/1000\n",
      "298/298 - 0s - loss: 41.8345 - val_loss: 33.3342\n",
      "Epoch 287/1000\n",
      "298/298 - 0s - loss: 40.1833 - val_loss: 33.5219\n",
      "Epoch 288/1000\n",
      "298/298 - 0s - loss: 42.5633 - val_loss: 45.5246\n",
      "Epoch 289/1000\n",
      "298/298 - 0s - loss: 43.4740 - val_loss: 32.2915\n",
      "Epoch 290/1000\n",
      "298/298 - 0s - loss: 40.7724 - val_loss: 35.0065\n",
      "Epoch 291/1000\n",
      "298/298 - 0s - loss: 40.1270 - val_loss: 41.1526\n",
      "Epoch 292/1000\n",
      "298/298 - 0s - loss: 41.5003 - val_loss: 35.0315\n",
      "Epoch 293/1000\n",
      "298/298 - 0s - loss: 39.4004 - val_loss: 33.8747\n",
      "Epoch 294/1000\n",
      "298/298 - 0s - loss: 41.5784 - val_loss: 31.3118\n",
      "Epoch 295/1000\n",
      "298/298 - 0s - loss: 38.1686 - val_loss: 31.1514\n",
      "Epoch 296/1000\n",
      "298/298 - 0s - loss: 38.6330 - val_loss: 37.5739\n",
      "Epoch 297/1000\n",
      "298/298 - 0s - loss: 38.8436 - val_loss: 30.6906\n",
      "Epoch 298/1000\n",
      "298/298 - 0s - loss: 37.6227 - val_loss: 32.6170\n",
      "Epoch 299/1000\n",
      "298/298 - 0s - loss: 36.6737 - val_loss: 30.3784\n",
      "Epoch 300/1000\n",
      "298/298 - 0s - loss: 36.7113 - val_loss: 30.9689\n",
      "Epoch 301/1000\n",
      "298/298 - 0s - loss: 36.3901 - val_loss: 31.8580\n",
      "Epoch 302/1000\n",
      "298/298 - 0s - loss: 36.4300 - val_loss: 29.8985\n",
      "Epoch 303/1000\n",
      "298/298 - 0s - loss: 36.6609 - val_loss: 31.8773\n",
      "Epoch 304/1000\n",
      "298/298 - 0s - loss: 39.6073 - val_loss: 29.4928\n",
      "Epoch 305/1000\n",
      "298/298 - 0s - loss: 37.2211 - val_loss: 29.9193\n",
      "Epoch 306/1000\n",
      "298/298 - 0s - loss: 38.2181 - val_loss: 42.4494\n",
      "Epoch 307/1000\n",
      "298/298 - 0s - loss: 41.9627 - val_loss: 36.3420\n",
      "Epoch 308/1000\n",
      "298/298 - 0s - loss: 35.2754 - val_loss: 29.0452\n",
      "Epoch 309/1000\n",
      "298/298 - 0s - loss: 34.5570 - val_loss: 28.5060\n",
      "Epoch 310/1000\n",
      "298/298 - 0s - loss: 35.0860 - val_loss: 28.4189\n",
      "Epoch 311/1000\n",
      "298/298 - 0s - loss: 34.4839 - val_loss: 29.8177\n",
      "Epoch 312/1000\n",
      "298/298 - 0s - loss: 37.1565 - val_loss: 27.9970\n",
      "Epoch 313/1000\n",
      "298/298 - 0s - loss: 38.1949 - val_loss: 34.7456\n",
      "Epoch 314/1000\n",
      "298/298 - 0s - loss: 35.7598 - val_loss: 29.5360\n",
      "Epoch 315/1000\n",
      "298/298 - 0s - loss: 34.7382 - val_loss: 30.6052\n",
      "Epoch 316/1000\n",
      "298/298 - 0s - loss: 34.0591 - val_loss: 29.3044\n",
      "Epoch 317/1000\n",
      "Restoring model weights from the end of the best epoch.\n",
      "298/298 - 0s - loss: 32.9764 - val_loss: 29.1071\n",
      "Epoch 00317: early stopping\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x22a9acc8608>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Activation\n",
    "import pandas as pd\n",
    "import io\n",
    "import os\n",
    "import requests\n",
    "import numpy as np\n",
    "from sklearn import metrics\n",
    "\n",
    "df = pd.read_csv(\n",
    "    \"https://data.heatonresearch.com/data/t81-558/auto-mpg.csv\", \n",
    "    na_values=['NA', '?'])\n",
    "\n",
    "cars = df['name']\n",
    "\n",
    "# Handle missing value\n",
    "df['horsepower'] = df['horsepower'].fillna(df['horsepower'].median())\n",
    "\n",
    "# Pandas to Numpy\n",
    "x = df[['cylinders', 'displacement', 'horsepower', 'weight',\n",
    "       'acceleration', 'year', 'origin']].values\n",
    "y = df['mpg'].values # regression\n",
    "\n",
    "# Split into validation and training sets\n",
    "x_train, x_test, y_train, y_test = train_test_split(    \n",
    "    x, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Build the neural network\n",
    "model = Sequential()\n",
    "model.add(Dense(25, input_dim=x.shape[1], activation='relu')) # Hidden 1\n",
    "model.add(Dense(10, activation='relu')) # Hidden 2\n",
    "model.add(Dense(1)) # Output\n",
    "model.compile(loss='mean_squared_error', optimizer='adam')\n",
    "\n",
    "monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, \n",
    "        patience=5, verbose=1, mode='auto',\n",
    "        restore_best_weights=True)\n",
    "model.fit(x_train,y_train,validation_data=(x_test,y_test),\n",
    "        callbacks=[monitor], verbose=2,epochs=1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we evaluate the error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final score (RMSE): 5.291219300799398\n"
     ]
    }
   ],
   "source": [
    "# Measure RMSE error.  RMSE is common for regression.\n",
    "pred = model.predict(x_test)\n",
    "score = np.sqrt(metrics.mean_squared_error(pred,y_test))\n",
    "print(f\"Final score (RMSE): {score}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3.9 (tensorflow)",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
